import axios from 'axios'

const instance = axios.create({
  baseURL: '/api',
  timeout: 10000,
})

instance.interceptors.request.use(config => {
  let uid = localStorage.getItem('wms_user_id')
  if (!uid) {
    uid = 'user_' + Math.random().toString(36).slice(2)
    localStorage.setItem('wms_user_id', uid)
  }
  config.headers['user-id'] = uid
  return config
})

instance.interceptors.response.use(
  res => res,
  err => {
    // 统一错误处理
    return Promise.reject(err)
  }
)

export const sendMessage = (content: string) => instance.post('/chat/send', { content })
export const getHistory = () => instance.get('/chat/history') 